from django.contrib.auth.models import User
from django.db import models


class Ware(models.Model):
    """
    货物属性
    name名字
    price价格
    isDealed是否交易成功
    origin_owner 原主人
    now_owner 现主人
    comment fk
    """
    name = models.TextField()
    price = models.FloatField()
    isDealed = models.BooleanField()
    origin_owner = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="bought_wares")
    now_owner = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="sold_wares")
    photo_path = models.TextField(default="image=default_ware.jpg")


class Notice(models.Model):
    """
    提示
    owner
    content
    """
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="owner_notices")
    title = models.TextField()
    content = models.TextField()


class Topic(models.Model):
    title = models.TextField(primary_key=True)
    total_floor = models.IntegerField(default=1)


class Comment(models.Model):
    """
    论坛的大众聊天
    例如贴吧
    """
    speaker = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="speaker_comments")
    content = models.TextField()
    response_target = models.IntegerField(default=-1)  # 返回回复的楼层，如果没有则为-1
    floor = models.IntegerField()  # 从0开始？
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name="comments")


class Question(models.Model):
    speaker = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="speaker_questions")
    content = models.TextField()
    target_ware = models.ForeignKey(Ware, on_delete=models.DO_NOTHING, related_name="ware_questions")


class Answer(models.Model):
    speaker = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="speaker_answers")
    content = models.TextField()
    target_question = models.ForeignKey(Question, on_delete=models.DO_NOTHING, related_name="its_answers")


class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True, verbose_name="userProfile", on_delete=models.CASCADE,
                                related_name="user_profile")
    gender = models.BooleanField()  # True is male, False is female
    nickname = models.TextField(default="")
    qq = models.TextField(default="")
    photo_path = models.TextField(default="image/default_user.png")


def create_user(username: str, password: str, gender: bool, qq: str, nickname: str,
                photo_path="image/default_user.png") -> None:
    origin_user = User.objects.create_user(username=username, password=password)
    extra_profile = UserProfile.objects.create(user=origin_user, gender=gender, nickname=nickname, qq=qq,
                                               photo_path=photo_path)
